package com.facebook.imagepipeline.animated.impl;

import android.app.ActivityManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import android.support.v4.util.SparseArrayCompat;
import bolts.Continuation;
import bolts.Task;
import com.facebook.common.executors.SerialExecutorService;
import com.facebook.common.internal.VisibleForTesting;
import com.facebook.common.logging.FLog;
import com.facebook.common.references.CloseableReference;
import com.facebook.common.references.ResourceReleaser;
import com.facebook.common.time.MonotonicClock;
import com.facebook.imagepipeline.animated.base.AnimatedDrawableBackend;
import com.facebook.imagepipeline.animated.base.AnimatedDrawableCachingBackend;
import com.facebook.imagepipeline.animated.base.AnimatedDrawableFrameInfo;
import com.facebook.imagepipeline.animated.base.AnimatedDrawableFrameInfo.DisposalMethod;
import com.facebook.imagepipeline.animated.base.AnimatedDrawableOptions;
import com.facebook.imagepipeline.animated.base.AnimatedImageResult;
import com.facebook.imagepipeline.animated.base.DelegatingAnimatedDrawableBackend;
import com.facebook.imagepipeline.animated.util.AnimatedDrawableUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.concurrent.GuardedBy;

public class AnimatedDrawableCachingBackendImpl extends DelegatingAnimatedDrawableBackend
  implements AnimatedDrawableCachingBackend
{
  private static final int PREFETCH_FRAMES = 3;
  private static final Class<?> TAG = AnimatedDrawableCachingBackendImpl.class;
  private static final AtomicInteger sTotalBitmaps = new AtomicInteger();
  private final ActivityManager mActivityManager;
  private final AnimatedDrawableBackend mAnimatedDrawableBackend;
  private final AnimatedDrawableOptions mAnimatedDrawableOptions;
  private final AnimatedDrawableUtil mAnimatedDrawableUtil;
  private final AnimatedImageCompositor mAnimatedImageCompositor;
  private final double mApproxKiloBytesToHoldAllFrames;

  @GuardedBy("this")
  private final WhatToKeepCachedArray mBitmapsToKeepCached;

  @GuardedBy("this")
  private final SparseArrayCompat<CloseableReference<Bitmap>> mCachedBitmaps;

  @GuardedBy("ui-thread")
  private int mCurrentFrameIndex;

  @GuardedBy("this")
  private final SparseArrayCompat<Task<Object>> mDecodesInFlight;
  private final SerialExecutorService mExecutorService;

  @GuardedBy("this")
  private final List<Bitmap> mFreeBitmaps;
  private final double mMaximumKiloBytes;
  private final MonotonicClock mMonotonicClock;
  private final ResourceReleaser<Bitmap> mResourceReleaserForBitmaps;

  public AnimatedDrawableCachingBackendImpl(SerialExecutorService paramSerialExecutorService, ActivityManager paramActivityManager, AnimatedDrawableUtil paramAnimatedDrawableUtil, MonotonicClock paramMonotonicClock, AnimatedDrawableBackend paramAnimatedDrawableBackend, AnimatedDrawableOptions paramAnimatedDrawableOptions)
  {
    super(paramAnimatedDrawableBackend);
    this.mExecutorService = paramSerialExecutorService;
    this.mActivityManager = paramActivityManager;
    this.mAnimatedDrawableUtil = paramAnimatedDrawableUtil;
    this.mMonotonicClock = paramMonotonicClock;
    this.mAnimatedDrawableBackend = paramAnimatedDrawableBackend;
    this.mAnimatedDrawableOptions = paramAnimatedDrawableOptions;
    if (paramAnimatedDrawableOptions.maximumBytes >= 0);
    for (double d = paramAnimatedDrawableOptions.maximumBytes / 1024; ; d = getDefaultMaxBytes(paramActivityManager) / 1024)
    {
      this.mMaximumKiloBytes = d;
      this.mAnimatedImageCompositor = new AnimatedImageCompositor(paramAnimatedDrawableBackend, new AnimatedImageCompositor.Callback()
      {
        public CloseableReference<Bitmap> getCachedBitmap(int paramAnonymousInt)
        {
          return AnimatedDrawableCachingBackendImpl.this.getCachedOrPredecodedFrame(paramAnonymousInt);
        }

        public void onIntermediateResult(int paramAnonymousInt, Bitmap paramAnonymousBitmap)
        {
          AnimatedDrawableCachingBackendImpl.this.maybeCacheBitmapDuringRender(paramAnonymousInt, paramAnonymousBitmap);
        }
      });
      this.mResourceReleaserForBitmaps = new ResourceReleaser()
      {
        public void release(Bitmap paramAnonymousBitmap)
        {
          AnimatedDrawableCachingBackendImpl.this.releaseBitmapInternal(paramAnonymousBitmap);
        }
      };
      this.mFreeBitmaps = new ArrayList();
      this.mDecodesInFlight = new SparseArrayCompat(10);
      this.mCachedBitmaps = new SparseArrayCompat(10);
      this.mBitmapsToKeepCached = new WhatToKeepCachedArray(this.mAnimatedDrawableBackend.getFrameCount());
      this.mApproxKiloBytesToHoldAllFrames = (4 * (this.mAnimatedDrawableBackend.getRenderedWidth() * this.mAnimatedDrawableBackend.getRenderedHeight() / 1024 * this.mAnimatedDrawableBackend.getFrameCount()));
      return;
    }
  }

  private void cancelFuturesOutsideOfRange(int paramInt1, int paramInt2)
  {
    int i = 0;
    while (true)
      try
      {
        if (i >= this.mDecodesInFlight.size())
          break;
        if (AnimatedDrawableUtil.isOutsideRange(paramInt1, paramInt2, this.mDecodesInFlight.keyAt(i)))
        {
          ((Task)this.mDecodesInFlight.valueAt(i));
          this.mDecodesInFlight.removeAt(i);
        }
      }
      finally
      {
      }
  }

  private void copyAndCacheBitmapDuringRendering(int paramInt, Bitmap paramBitmap)
  {
    CloseableReference localCloseableReference = obtainBitmapInternal();
    try
    {
      Canvas localCanvas = new Canvas((Bitmap)localCloseableReference.get());
      localCanvas.drawColor(0, PorterDuff.Mode.SRC);
      localCanvas.drawBitmap(paramBitmap, 0.0F, 0.0F, null);
      maybeCacheRenderedBitmap(paramInt, localCloseableReference);
      return;
    }
    finally
    {
      localCloseableReference.close();
    }
  }

  private Bitmap createNewBitmap()
  {
    FLog.v(TAG, "Creating new bitmap");
    sTotalBitmaps.incrementAndGet();
    FLog.v(TAG, "Total bitmaps: %d", Integer.valueOf(sTotalBitmaps.get()));
    return Bitmap.createBitmap(this.mAnimatedDrawableBackend.getRenderedWidth(), this.mAnimatedDrawableBackend.getRenderedHeight(), Bitmap.Config.ARGB_8888);
  }

  private void doPrefetch(int paramInt1, int paramInt2)
  {
    int i = 0;
    while (true)
    {
      int j;
      if (i < paramInt2)
        j = paramInt1 + i;
      try
      {
        final int k = j % this.mAnimatedDrawableBackend.getFrameCount();
        boolean bool = hasCachedOrPredecodedFrame(k);
        Task localTask1 = (Task)this.mDecodesInFlight.get(k);
        if ((!bool) && (localTask1 == null))
        {
          final Task localTask2 = Task.call(new Callable()
          {
            public Object call()
            {
              AnimatedDrawableCachingBackendImpl.this.runPrefetch(k);
              return null;
            }
          }
          , this.mExecutorService);
          this.mDecodesInFlight.put(k, localTask2);
          localTask2.continueWith(new Continuation()
          {
            public Object then(Task<Object> paramAnonymousTask)
              throws Exception
            {
              AnimatedDrawableCachingBackendImpl.this.onFutureFinished(localTask2, k);
              return null;
            }
          });
        }
        i++;
        continue;
        return;
      }
      finally
      {
      }
    }
  }

  private void dropBitmapsThatShouldNotBeCached()
  {
    int i = 0;
    while (true)
      try
      {
        if (i >= this.mCachedBitmaps.size())
          break;
        int j = this.mCachedBitmaps.keyAt(i);
        if (!this.mBitmapsToKeepCached.get(j))
        {
          CloseableReference localCloseableReference = (CloseableReference)this.mCachedBitmaps.valueAt(i);
          this.mCachedBitmaps.removeAt(i);
          localCloseableReference.close();
        }
      }
      finally
      {
      }
  }

  // ERROR //
  private CloseableReference<Bitmap> getBitmapForFrameInternal(int paramInt, boolean paramBoolean)
  {
    // Byte code:
    //   0: iconst_0
    //   1: istore_3
    //   2: aload_0
    //   3: getfield 72	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:mMonotonicClock	Lcom/facebook/common/time/MonotonicClock;
    //   6: invokeinterface 297 1 0
    //   11: lstore 4
    //   13: aload_0
    //   14: monitorenter
    //   15: aload_0
    //   16: getfield 125	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:mBitmapsToKeepCached	Lcom/facebook/imagepipeline/animated/impl/WhatToKeepCachedArray;
    //   19: iload_1
    //   20: iconst_1
    //   21: invokevirtual 301	com/facebook/imagepipeline/animated/impl/WhatToKeepCachedArray:set	(IZ)V
    //   24: aload_0
    //   25: iload_1
    //   26: invokespecial 149	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:getCachedOrPredecodedFrame	(I)Lcom/facebook/common/references/CloseableReference;
    //   29: astore 11
    //   31: aload 11
    //   33: ifnull +80 -> 113
    //   36: aload_0
    //   37: monitorexit
    //   38: aload_0
    //   39: getfield 72	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:mMonotonicClock	Lcom/facebook/common/time/MonotonicClock;
    //   42: invokeinterface 297 1 0
    //   47: lload 4
    //   49: lsub
    //   50: lstore 12
    //   52: lload 12
    //   54: ldc2_w 302
    //   57: lcmp
    //   58: ifle +32 -> 90
    //   61: iconst_0
    //   62: ifeq +31 -> 93
    //   65: ldc_w 305
    //   68: astore 14
    //   70: getstatic 53	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:TAG	Ljava/lang/Class;
    //   73: ldc_w 307
    //   76: iload_1
    //   77: invokestatic 246	java/lang/Integer:valueOf	(I)Ljava/lang/Integer;
    //   80: lload 12
    //   82: invokestatic 312	java/lang/Long:valueOf	(J)Ljava/lang/Long;
    //   85: aload 14
    //   87: invokestatic 315	com/facebook/common/logging/FLog:v	(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
    //   90: aload 11
    //   92: areturn
    //   93: iconst_0
    //   94: ifeq +11 -> 105
    //   97: ldc_w 317
    //   100: astore 14
    //   102: goto -32 -> 70
    //   105: ldc_w 319
    //   108: astore 14
    //   110: goto -40 -> 70
    //   113: aload_0
    //   114: monitorexit
    //   115: iload_2
    //   116: ifeq +195 -> 311
    //   119: iconst_1
    //   120: istore_3
    //   121: aload_0
    //   122: invokespecial 189	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:obtainBitmapInternal	()Lcom/facebook/common/references/CloseableReference;
    //   125: astore 19
    //   127: aload_0
    //   128: getfield 95	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:mAnimatedImageCompositor	Lcom/facebook/imagepipeline/animated/impl/AnimatedImageCompositor;
    //   131: iload_1
    //   132: aload 19
    //   134: invokevirtual 197	com/facebook/common/references/CloseableReference:get	()Ljava/lang/Object;
    //   137: checkcast 199	android/graphics/Bitmap
    //   140: invokevirtual 322	com/facebook/imagepipeline/animated/impl/AnimatedImageCompositor:renderFrame	(ILandroid/graphics/Bitmap;)V
    //   143: aload_0
    //   144: iload_1
    //   145: aload 19
    //   147: invokespecial 220	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:maybeCacheRenderedBitmap	(ILcom/facebook/common/references/CloseableReference;)V
    //   150: aload 19
    //   152: invokevirtual 325	com/facebook/common/references/CloseableReference:clone	()Lcom/facebook/common/references/CloseableReference;
    //   155: astore 21
    //   157: aload 19
    //   159: invokevirtual 223	com/facebook/common/references/CloseableReference:close	()V
    //   162: aload_0
    //   163: getfield 72	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:mMonotonicClock	Lcom/facebook/common/time/MonotonicClock;
    //   166: invokeinterface 297 1 0
    //   171: lload 4
    //   173: lsub
    //   174: lstore 22
    //   176: lload 22
    //   178: ldc2_w 302
    //   181: lcmp
    //   182: ifle +32 -> 214
    //   185: iload_3
    //   186: ifeq +95 -> 281
    //   189: ldc_w 305
    //   192: astore 24
    //   194: getstatic 53	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:TAG	Ljava/lang/Class;
    //   197: ldc_w 307
    //   200: iload_1
    //   201: invokestatic 246	java/lang/Integer:valueOf	(I)Ljava/lang/Integer;
    //   204: lload 22
    //   206: invokestatic 312	java/lang/Long:valueOf	(J)Ljava/lang/Long;
    //   209: aload 24
    //   211: invokestatic 315	com/facebook/common/logging/FLog:v	(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
    //   214: aload 21
    //   216: areturn
    //   217: astore 10
    //   219: aload_0
    //   220: monitorexit
    //   221: aload 10
    //   223: athrow
    //   224: astore 6
    //   226: aload_0
    //   227: getfield 72	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:mMonotonicClock	Lcom/facebook/common/time/MonotonicClock;
    //   230: invokeinterface 297 1 0
    //   235: lload 4
    //   237: lsub
    //   238: lstore 7
    //   240: lload 7
    //   242: ldc2_w 302
    //   245: lcmp
    //   246: ifle +32 -> 278
    //   249: iload_3
    //   250: ifeq +142 -> 392
    //   253: ldc_w 305
    //   256: astore 9
    //   258: getstatic 53	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:TAG	Ljava/lang/Class;
    //   261: ldc_w 307
    //   264: iload_1
    //   265: invokestatic 246	java/lang/Integer:valueOf	(I)Ljava/lang/Integer;
    //   268: lload 7
    //   270: invokestatic 312	java/lang/Long:valueOf	(J)Ljava/lang/Long;
    //   273: aload 9
    //   275: invokestatic 315	com/facebook/common/logging/FLog:v	(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
    //   278: aload 6
    //   280: athrow
    //   281: iconst_0
    //   282: ifeq +11 -> 293
    //   285: ldc_w 317
    //   288: astore 24
    //   290: goto -96 -> 194
    //   293: ldc_w 319
    //   296: astore 24
    //   298: goto -104 -> 194
    //   301: astore 20
    //   303: aload 19
    //   305: invokevirtual 223	com/facebook/common/references/CloseableReference:close	()V
    //   308: aload 20
    //   310: athrow
    //   311: aload_0
    //   312: getfield 72	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:mMonotonicClock	Lcom/facebook/common/time/MonotonicClock;
    //   315: invokeinterface 297 1 0
    //   320: lload 4
    //   322: lsub
    //   323: lstore 15
    //   325: lload 15
    //   327: ldc2_w 302
    //   330: lcmp
    //   331: istore 17
    //   333: aconst_null
    //   334: astore 11
    //   336: iload 17
    //   338: ifle -248 -> 90
    //   341: iconst_0
    //   342: ifeq +30 -> 372
    //   345: ldc_w 305
    //   348: astore 18
    //   350: getstatic 53	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:TAG	Ljava/lang/Class;
    //   353: ldc_w 307
    //   356: iload_1
    //   357: invokestatic 246	java/lang/Integer:valueOf	(I)Ljava/lang/Integer;
    //   360: lload 15
    //   362: invokestatic 312	java/lang/Long:valueOf	(J)Ljava/lang/Long;
    //   365: aload 18
    //   367: invokestatic 315	com/facebook/common/logging/FLog:v	(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
    //   370: aconst_null
    //   371: areturn
    //   372: iconst_1
    //   373: ifeq +11 -> 384
    //   376: ldc_w 317
    //   379: astore 18
    //   381: goto -31 -> 350
    //   384: ldc_w 319
    //   387: astore 18
    //   389: goto -39 -> 350
    //   392: iconst_0
    //   393: ifeq +11 -> 404
    //   396: ldc_w 317
    //   399: astore 9
    //   401: goto -143 -> 258
    //   404: ldc_w 319
    //   407: astore 9
    //   409: goto -151 -> 258
    //
    // Exception table:
    //   from	to	target	type
    //   15	31	217	finally
    //   36	38	217	finally
    //   113	115	217	finally
    //   219	221	217	finally
    //   13	15	224	finally
    //   121	127	224	finally
    //   157	162	224	finally
    //   221	224	224	finally
    //   303	311	224	finally
    //   127	157	301	finally
  }

  private CloseableReference<Bitmap> getCachedOrPredecodedFrame(int paramInt)
  {
    try
    {
      Object localObject2 = CloseableReference.cloneOrNull((CloseableReference)this.mCachedBitmaps.get(paramInt));
      if (localObject2 == null)
      {
        CloseableReference localCloseableReference = this.mAnimatedDrawableBackend.getPreDecodedFrame(paramInt);
        localObject2 = localCloseableReference;
      }
      return localObject2;
    }
    finally
    {
    }
  }

  private static int getDefaultMaxBytes(ActivityManager paramActivityManager)
  {
    if (paramActivityManager.getMemoryClass() > 32)
      return 5242880;
    return 3145728;
  }

  private boolean hasCachedOrPredecodedFrame(int paramInt)
  {
    try
    {
      if (this.mCachedBitmaps.get(paramInt) == null)
      {
        boolean bool2 = this.mAnimatedDrawableBackend.hasPreDecodedFrame(paramInt);
        if (!bool2);
      }
      else
      {
        bool1 = true;
        return bool1;
      }
      boolean bool1 = false;
    }
    finally
    {
    }
  }

  private void maybeCacheBitmapDuringRender(int paramInt, Bitmap paramBitmap)
  {
    try
    {
      boolean bool = this.mBitmapsToKeepCached.get(paramInt);
      int i = 0;
      if (bool)
        if (this.mCachedBitmaps.get(paramInt) != null)
          break label48;
      label48: for (i = 1; ; i = 0)
      {
        if (i != 0)
          copyAndCacheBitmapDuringRendering(paramInt, paramBitmap);
        return;
      }
    }
    finally
    {
    }
  }

  private void maybeCacheRenderedBitmap(int paramInt, CloseableReference<Bitmap> paramCloseableReference)
  {
    try
    {
      boolean bool = this.mBitmapsToKeepCached.get(paramInt);
      if (!bool);
      while (true)
      {
        return;
        int i = this.mCachedBitmaps.indexOfKey(paramInt);
        if (i >= 0)
        {
          ((CloseableReference)this.mCachedBitmaps.valueAt(i)).close();
          this.mCachedBitmaps.removeAt(i);
        }
        this.mCachedBitmaps.put(paramInt, paramCloseableReference.clone());
      }
    }
    finally
    {
    }
  }

  private CloseableReference<Bitmap> obtainBitmapInternal()
  {
    try
    {
      long l1 = System.nanoTime();
      long l2 = l1 + TimeUnit.NANOSECONDS.convert(20L, TimeUnit.MILLISECONDS);
      while (true)
      {
        boolean bool = this.mFreeBitmaps.isEmpty();
        if ((bool) && (l1 < l2))
          try
          {
            TimeUnit.NANOSECONDS.timedWait(this, l2 - l1);
            long l3 = System.nanoTime();
            l1 = l3;
          }
          catch (InterruptedException localInterruptedException)
          {
            Thread.currentThread().interrupt();
            throw new RuntimeException(localInterruptedException);
          }
      }
    }
    finally
    {
    }
    if (this.mFreeBitmaps.isEmpty());
    for (Bitmap localBitmap = createNewBitmap(); ; localBitmap = (Bitmap)this.mFreeBitmaps.remove(-1 + this.mFreeBitmaps.size()))
      return CloseableReference.of(localBitmap, this.mResourceReleaserForBitmaps);
  }

  private void onFutureFinished(Task<?> paramTask, int paramInt)
  {
    try
    {
      int i = this.mDecodesInFlight.indexOfKey(paramInt);
      if ((i >= 0) && ((Task)this.mDecodesInFlight.valueAt(i) == paramTask))
      {
        this.mDecodesInFlight.removeAt(i);
        if (paramTask.getError() != null)
        {
          Class localClass = TAG;
          Exception localException = paramTask.getError();
          Object[] arrayOfObject = new Object[1];
          arrayOfObject[0] = Integer.valueOf(paramInt);
          FLog.v(localClass, localException, "Failed to render frame %d", arrayOfObject);
        }
      }
      return;
    }
    finally
    {
    }
  }

  // ERROR //
  private void runPrefetch(int paramInt)
  {
    // Byte code:
    //   0: aload_0
    //   1: monitorenter
    //   2: aload_0
    //   3: getfield 125	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:mBitmapsToKeepCached	Lcom/facebook/imagepipeline/animated/impl/WhatToKeepCachedArray;
    //   6: iload_1
    //   7: invokevirtual 289	com/facebook/imagepipeline/animated/impl/WhatToKeepCachedArray:get	(I)Z
    //   10: ifne +6 -> 16
    //   13: aload_0
    //   14: monitorexit
    //   15: return
    //   16: aload_0
    //   17: iload_1
    //   18: invokespecial 264	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:hasCachedOrPredecodedFrame	(I)Z
    //   21: ifeq +11 -> 32
    //   24: aload_0
    //   25: monitorexit
    //   26: return
    //   27: astore_2
    //   28: aload_0
    //   29: monitorexit
    //   30: aload_2
    //   31: athrow
    //   32: aload_0
    //   33: monitorexit
    //   34: aload_0
    //   35: getfield 74	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:mAnimatedDrawableBackend	Lcom/facebook/imagepipeline/animated/base/AnimatedDrawableBackend;
    //   38: iload_1
    //   39: invokeinterface 332 2 0
    //   44: astore_3
    //   45: aload_3
    //   46: ifnull +14 -> 60
    //   49: aload_0
    //   50: iload_1
    //   51: aload_3
    //   52: invokespecial 220	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:maybeCacheRenderedBitmap	(ILcom/facebook/common/references/CloseableReference;)V
    //   55: aload_3
    //   56: invokestatic 418	com/facebook/common/references/CloseableReference:closeSafely	(Lcom/facebook/common/references/CloseableReference;)V
    //   59: return
    //   60: aload_0
    //   61: invokespecial 189	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:obtainBitmapInternal	()Lcom/facebook/common/references/CloseableReference;
    //   64: astore 5
    //   66: aload_0
    //   67: getfield 95	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:mAnimatedImageCompositor	Lcom/facebook/imagepipeline/animated/impl/AnimatedImageCompositor;
    //   70: iload_1
    //   71: aload 5
    //   73: invokevirtual 197	com/facebook/common/references/CloseableReference:get	()Ljava/lang/Object;
    //   76: checkcast 199	android/graphics/Bitmap
    //   79: invokevirtual 322	com/facebook/imagepipeline/animated/impl/AnimatedImageCompositor:renderFrame	(ILandroid/graphics/Bitmap;)V
    //   82: aload_0
    //   83: iload_1
    //   84: aload 5
    //   86: invokespecial 220	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:maybeCacheRenderedBitmap	(ILcom/facebook/common/references/CloseableReference;)V
    //   89: getstatic 53	com/facebook/imagepipeline/animated/impl/AnimatedDrawableCachingBackendImpl:TAG	Ljava/lang/Class;
    //   92: ldc_w 420
    //   95: iload_1
    //   96: invokestatic 246	java/lang/Integer:valueOf	(I)Ljava/lang/Integer;
    //   99: invokestatic 249	com/facebook/common/logging/FLog:v	(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/Object;)V
    //   102: aload 5
    //   104: invokevirtual 223	com/facebook/common/references/CloseableReference:close	()V
    //   107: goto -52 -> 55
    //   110: astore 4
    //   112: aload_3
    //   113: invokestatic 418	com/facebook/common/references/CloseableReference:closeSafely	(Lcom/facebook/common/references/CloseableReference;)V
    //   116: aload 4
    //   118: athrow
    //   119: astore 6
    //   121: aload 5
    //   123: invokevirtual 223	com/facebook/common/references/CloseableReference:close	()V
    //   126: aload 6
    //   128: athrow
    //
    // Exception table:
    //   from	to	target	type
    //   2	15	27	finally
    //   16	26	27	finally
    //   28	30	27	finally
    //   32	34	27	finally
    //   49	55	110	finally
    //   60	66	110	finally
    //   102	107	110	finally
    //   121	129	110	finally
    //   66	102	119	finally
  }

  private void schedulePrefetches()
  {
    int i = 1;
    while (true)
    {
      int j;
      try
      {
        int i4;
        if (this.mAnimatedDrawableBackend.getFrameInfo(this.mCurrentFrameIndex).disposalMethod == AnimatedDrawableFrameInfo.DisposalMethod.DISPOSE_TO_PREVIOUS)
        {
          j = i;
          int k = this.mCurrentFrameIndex;
          if (j != 0)
          {
            m = i;
            int n = Math.max(0, k - m);
            if (!this.mAnimatedDrawableOptions.allowPrefetching)
              continue;
            i1 = 3;
            break label231;
            int i2 = Math.max(i1, i);
            int i3 = (n + i2) % this.mAnimatedDrawableBackend.getFrameCount();
            cancelFuturesOutsideOfRange(n, i3);
            if (!shouldKeepAllFramesInMemory())
            {
              this.mBitmapsToKeepCached.setAll(true);
              this.mBitmapsToKeepCached.removeOutsideRange(n, i3);
              i4 = n;
              if (i4 >= 0)
              {
                if (this.mCachedBitmaps.get(i4) == null)
                  continue;
                this.mBitmapsToKeepCached.set(i4, true);
              }
              dropBitmapsThatShouldNotBeCached();
            }
            if (!this.mAnimatedDrawableOptions.allowPrefetching)
              continue;
            doPrefetch(n, i2);
          }
        }
        else
        {
          j = 0;
          continue;
        }
        int m = 0;
        continue;
        int i1 = 0;
        break label231;
        i = 0;
        continue;
        i4--;
        continue;
        cancelFuturesOutsideOfRange(this.mCurrentFrameIndex, this.mCurrentFrameIndex);
        continue;
      }
      finally
      {
      }
      label231: if (j == 0);
    }
  }

  private boolean shouldKeepAllFramesInMemory()
  {
    if (this.mAnimatedDrawableOptions.forceKeepAllFramesInMemory);
    while (this.mApproxKiloBytesToHoldAllFrames < this.mMaximumKiloBytes)
      return true;
    return false;
  }

  public void appendDebugOptionString(StringBuilder paramStringBuilder)
  {
    if (this.mAnimatedDrawableOptions.forceKeepAllFramesInMemory)
    {
      paramStringBuilder.append("Pinned To Memory");
      if ((shouldKeepAllFramesInMemory()) && (this.mAnimatedDrawableOptions.allowPrefetching))
        paramStringBuilder.append(" MT");
      return;
    }
    if (this.mApproxKiloBytesToHoldAllFrames < this.mMaximumKiloBytes)
      paramStringBuilder.append("within ");
    while (true)
    {
      this.mAnimatedDrawableUtil.appendMemoryString(paramStringBuilder, (int)this.mMaximumKiloBytes);
      break;
      paramStringBuilder.append("exceeds ");
    }
  }

  public void dropCaches()
  {
    try
    {
      this.mBitmapsToKeepCached.setAll(false);
      dropBitmapsThatShouldNotBeCached();
      Iterator localIterator = this.mFreeBitmaps.iterator();
      while (localIterator.hasNext())
      {
        ((Bitmap)localIterator.next()).recycle();
        sTotalBitmaps.decrementAndGet();
      }
    }
    finally
    {
    }
    this.mFreeBitmaps.clear();
    this.mAnimatedDrawableBackend.dropCaches();
    FLog.v(TAG, "Total bitmaps: %d", Integer.valueOf(sTotalBitmaps.get()));
  }

  protected void finalize()
    throws Throwable
  {
    try
    {
      super.finalize();
      if (this.mCachedBitmaps.size() > 0)
        FLog.d(TAG, "Finalizing with rendered bitmaps");
      sTotalBitmaps.addAndGet(-this.mFreeBitmaps.size());
      this.mFreeBitmaps.clear();
      return;
    }
    finally
    {
    }
  }

  public AnimatedDrawableCachingBackend forNewBounds(Rect paramRect)
  {
    AnimatedDrawableBackend localAnimatedDrawableBackend = this.mAnimatedDrawableBackend.forNewBounds(paramRect);
    if (localAnimatedDrawableBackend == this.mAnimatedDrawableBackend)
      return this;
    return new AnimatedDrawableCachingBackendImpl(this.mExecutorService, this.mActivityManager, this.mAnimatedDrawableUtil, this.mMonotonicClock, localAnimatedDrawableBackend, this.mAnimatedDrawableOptions);
  }

  public CloseableReference<Bitmap> getBitmapForFrame(int paramInt)
  {
    this.mCurrentFrameIndex = paramInt;
    CloseableReference localCloseableReference = getBitmapForFrameInternal(paramInt, false);
    schedulePrefetches();
    return localCloseableReference;
  }

  @VisibleForTesting
  CloseableReference<Bitmap> getBitmapForFrameBlocking(int paramInt)
  {
    this.mCurrentFrameIndex = paramInt;
    CloseableReference localCloseableReference = getBitmapForFrameInternal(paramInt, true);
    schedulePrefetches();
    return localCloseableReference;
  }

  @VisibleForTesting
  Map<Integer, Task<?>> getDecodesInFlight()
  {
    try
    {
      HashMap localHashMap = new HashMap();
      for (int i = 0; i < this.mDecodesInFlight.size(); i++)
        localHashMap.put(Integer.valueOf(this.mDecodesInFlight.keyAt(i)), this.mDecodesInFlight.valueAt(i));
      return localHashMap;
    }
    finally
    {
    }
  }

  @VisibleForTesting
  Set<Integer> getFramesCached()
  {
    try
    {
      HashSet localHashSet = new HashSet();
      for (int i = 0; i < this.mCachedBitmaps.size(); i++)
        localHashSet.add(Integer.valueOf(this.mCachedBitmaps.keyAt(i)));
      return localHashSet;
    }
    finally
    {
    }
  }

  public int getMemoryUsage()
  {
    int i = 0;
    while (true)
    {
      try
      {
        Iterator localIterator = this.mFreeBitmaps.iterator();
        if (localIterator.hasNext())
        {
          Bitmap localBitmap = (Bitmap)localIterator.next();
          i += this.mAnimatedDrawableUtil.getSizeOfBitmap(localBitmap);
          continue;
          if (j < this.mCachedBitmaps.size())
          {
            CloseableReference localCloseableReference = (CloseableReference)this.mCachedBitmaps.valueAt(j);
            i += this.mAnimatedDrawableUtil.getSizeOfBitmap((Bitmap)localCloseableReference.get());
            j++;
            continue;
          }
          return i + this.mAnimatedDrawableBackend.getMemoryUsage();
        }
      }
      finally
      {
      }
      int j = 0;
    }
  }

  public CloseableReference<Bitmap> getPreviewBitmap()
  {
    return getAnimatedImageResult().getPreviewBitmap();
  }

  void releaseBitmapInternal(Bitmap paramBitmap)
  {
    try
    {
      this.mFreeBitmaps.add(paramBitmap);
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public void renderFrame(int paramInt, Canvas paramCanvas)
  {
    throw new IllegalStateException();
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.facebook.imagepipeline.animated.impl.AnimatedDrawableCachingBackendImpl
 * JD-Core Version:    0.6.2
 */